package com.yc.vote.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.yc.vote.bean.Topics;
import com.yc.vote.bean.UserInfo;
import com.yc.vote.service.ITopicsService;
import com.yc.vote.service.IUserInfoService;
import com.yc.vote.service.impl.TopicsServiceImpl;
import com.yc.vote.util.RequestParamUtil;

@WebServlet("/topic/*")
public class TopicsController extends BaseController {
	private static final long serialVersionUID = -8589897518575486250L;

	/**
	 * 添加投票主体
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object obj = request.getSession().getAttribute("currentLoginUser");
		if(obj == null) {
			this.send(response, 600, "未登录");
			return;
		}
		UserInfo uf = (UserInfo) obj;
		Topics tp = RequestParamUtil.getParams(request, Topics.class);
		tp.setUsid(uf.getUsid());
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		ITopicsService topicsService = (ITopicsService) context.getBean("topicsServiceImpl");
		int result = topicsService.add(tp);
		if(result > 0) {
			this.send(response, 200, "成功");
			return;
		}
		this.send(response, 500, "失败");
	}
	
	/**
	 * 查询所有投票主题信息
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		ITopicsService topicsService = (ITopicsService) context.getBean("topicsServiceImpl");
		List<Topics> list = topicsService.findAll();
		if(list != null && !list.isEmpty()) {
			this.send(response, 200, list);
		}
		this.send(response, 600, "暂无数据");
	}
	
	/**
	 * 根据主题编号查询主题
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void findByTid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String tid = request.getParameter("tid");
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		ITopicsService topicsService = (ITopicsService) context.getBean("topicsServiceImpl");
		Topics topics = topicsService.findByTid(tid);
		if(topics != null ) {
			this.send(response, 200, topics);
			return;
		}
		this.send(response, 600, "暂无数据");
	}
	
	/**
	 * 投票
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void vote(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Object obj = request.getSession().getAttribute("currentLoginUser");
		if(obj == null) {
			this.send(response, 600, "未登录");
			return;
		}
		UserInfo uf = (UserInfo) obj;
		String tid = request.getParameter("tid");
		String iids = request.getParameter("iids");
		
		ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		ITopicsService topicsService = (ITopicsService) context.getBean("topicsServiceImpl");
		int result = topicsService.update(tid, uf.getUsid(),  iids);
		if(result > 0) {
			this.send(response, 200, "成功");
			return;
		}
		this.send(response, 500, "失败");
	}
}

